﻿using System;
using System.Collections.Generic;
using FluentIoC.Interfaces;

namespace FluentIoC.Configuration.Commands
{
    public class RegisterProperties
    {
        protected List<SetProperty> _setProperties = new List<SetProperty>();
        private Dictionary<string, string> _properties = new Dictionary<string, string>();

        internal Lifestyle Lifestyle { get; set; }

        internal Dictionary<string, string> Properties
        {
            get
            {
               foreach(var set in _setProperties)
               {
                   if(false == _properties.ContainsKey(set.Property))
                   {
                       _properties[set.Property] = set.Value;
                   }
               }

               return _properties;
            }
            set
            {
                _properties = value;
            }
        }
        
        public RegisterProperties WithProperties(Dictionary<string, string> properties)
        {
            Properties = properties;
            return this;
        }

        public RegisterProperties WithLifestyle(Lifestyle lifestyle)
        {
            Lifestyle = lifestyle;
            return this;
        }

        public RegisterProperties SetProperty<T>(Func<T, object> property)
        {
            _setProperties.Add(new SetProperty<T>(property));
            return this;
        }

        public RegisterProperties SetProperty<T>(Func<T, object> property, string serviceKey)
        {
            _setProperties.Add(new SetProperty<T>(property, serviceKey));
            return this;
        }

        public SetProperty SetProperty(string property)
        {
            var setProperty = new SetProperty(property, this);
            _setProperties.Add(setProperty);
            return setProperty;
        }

        public RegisterProperties WithProperties(params Action<Dictionary<string, string>>[] properties)
        {
            Properties = new Dictionary<string, string>();

            foreach (var property in properties)
            {
                property(Properties);
            }

            return this;
        }
    }
}
